﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchEdu.Framework.DataModel;
using SchEdu.Framework.Exceptions;
using SchEdu.Framework.Logging;

namespace SchEdu.Framework.Objective
{
    public class Performancer
    {
        private Logger logger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());

        protected Logger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

        SchEduData schEduData;

        public SchEduData SchEduData
        {
            get { return schEduData; }
            set { schEduData = value; }
        }

        SchEduDataSet.SolutionRow solution;

        public SchEduDataSet.SolutionRow Solution
        {
            get { return solution; }
            set { solution = value; }
        }

        ObjectiveList objectives = new ObjectiveList();

        public ObjectiveList Objectives
        {
            get { return objectives; }
            set { objectives = value; }
        }

        public Performancer(SchEduData schEduData, SchEduDataSet.SolutionRow solution, ObjectiveList objectives)
        {
            this.schEduData = schEduData;
            this.solution = solution;
            this.objectives = objectives;
        }
        public void Generate()
        {
            Logger.Debug(string.Format("Generating Performances for Solution {0}",solution.ID));

            OrderList orders = new OrderList(schEduData.Order);

            schEduData.RefreshAdditionalFields();

            foreach (SchEduDataSet.AllocationRow allocation in solution.GetAllocationRows())
            {
                SchEduDataSet.OrderRow order = allocation.OrderRow;
                if (order == null)
                {
                    continue;
                }

                if (order.StartDate >= allocation.StartDate)
                {
                    order.StartDate = allocation.StartDate;
                }
                if (order.EndDate <= allocation.EndDate)
                {
                    order.EndDate = allocation.EndDate;
                }

            }

           
            foreach (IObjective objective in objectives)
            {
                objective.SchEduData = schEduData;
                objective.Solution = solution;
                try
                {
                    objective.Run();
                }
                catch (ValidatorException ex)
                {
                    logger.Debug(ex);
                }
            }
        }
    }
}
